package com.ruoyi.web.controller.shortVedio;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.VideoParseResponse;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.common.utils.MiniprogramUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.IVideoParseLogService;
import com.ruoyi.system.service.XiaohongshuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 小红书视频解析控制器
 * @Author: JH-Ahua
 * @CreateTime: 2025/1/15
 */
@Api(tags = "短视频解析")
@RestController
@RequestMapping("/parse/xiaohongshu")
@CrossOrigin(origins = "*")
public class XiaohongshuController {
    
    @Autowired
    private XiaohongshuService xiaohongshuService;
    
    @Autowired
    private IVideoParseLogService videoParseLogService;
    
    @ApiOperation(value = "小红书视频图集去水印解析", notes = "解析小红书分享链接，获取视频或图集信息")
    @ApiImplicitParam(name = "url", value = "小红书分享链接或包含链接的文本", required = true, dataType = "string", paramType = "query")
    @Log(title = "小红书视频解析", businessType = BusinessType.XIAOHONGSHU_PARSE, operatorType = com.ruoyi.common.enums.OperatorType.MOBILE)
    @GetMapping("/parse")
    public VideoParseResponse parseXiaohongshuVideo(@RequestParam("url") String url, HttpServletRequest request) {
        long startTime = System.currentTimeMillis();
        
        // 获取小程序标识和用户信息
        String miniprogramId = MiniprogramUtils.getMiniprogramId(request);
        String userId = MiniprogramUtils.getUserId(request);
        String userIp = IpUtils.getIpAddr(request);
        String userAgent = request.getHeader("User-Agent");
        
        // 从输入字符串中提取小红书URL
        String extractedUrl = StringUtils.extractXiaohongshuUrl(url);
        
        // 解析视频
        VideoParseResponse response = xiaohongshuService.parseXiaohongshuVideo(extractedUrl);
        
        // 计算解析耗时
        long parseTime = System.currentTimeMillis() - startTime;
        
        // 记录日志
        try {
            videoParseLogService.recordVideoParseLog(
                miniprogramId, userId, userIp, "xiaohongshu", url, extractedUrl,
                response.getData(), response.getCode() == 200 ? 0 : 1,
                response.getCode() == 200 ? null : response.getMsg(),
                parseTime, userAgent
            );
        } catch (Exception e) {
            // 日志记录失败不影响主流程
            System.err.println("记录小红书解析日志失败: " + e.getMessage());
        }
        
        return response;
    }
    
    @ApiOperation(value = "小红书视频图集去水印解析(POST)", notes = "解析小红书分享链接，获取视频或图集信息")
    @Log(title = "小红书视频解析", businessType = BusinessType.XIAOHONGSHU_PARSE, operatorType = com.ruoyi.common.enums.OperatorType.MOBILE)
    @PostMapping("/parse")
    public VideoParseResponse parseXiaohongshuVideoPost(@RequestParam("url") String url, HttpServletRequest request) {
        long startTime = System.currentTimeMillis();
        
        // 获取小程序标识和用户信息
        String miniprogramId = MiniprogramUtils.getMiniprogramId(request);
        String userId = MiniprogramUtils.getUserId(request);
        String userIp = IpUtils.getIpAddr(request);
        String userAgent = request.getHeader("User-Agent");
        
        // 从输入字符串中提取小红书URL
        String extractedUrl = StringUtils.extractXiaohongshuUrl(url);
        
        // 解析视频
        VideoParseResponse response = xiaohongshuService.parseXiaohongshuVideo(extractedUrl);
        
        // 计算解析耗时
        long parseTime = System.currentTimeMillis() - startTime;
        
        // 记录日志
        try {
            videoParseLogService.recordVideoParseLog(
                miniprogramId, userId, userIp, "xiaohongshu", url, extractedUrl,
                response.getData(), response.getCode() == 200 ? 0 : 1,
                response.getCode() == 200 ? null : response.getMsg(),
                parseTime, userAgent
            );
        } catch (Exception e) {
            // 日志记录失败不影响主流程
            System.err.println("记录小红书解析日志失败: " + e.getMessage());
        }
        
        return response;
    }
}
